package com.org.base.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.org.base.ctrl.Constant;
import com.org.zp.R;

/**
 * 
 * @author Beanu
 * 
 */
public class LoadMoreListView extends ListView implements OnScrollListener {

	private static final String TAG = "LoadMoreListView";

	/**
	 * Listener that will receive notifications every time the list scrolls.
	 */
	private OnScrollListener mOnScrollListener;
	private LayoutInflater mInflater;

	private LinearLayout mFooterView;

	// Listener to process load more items when user reaches the end of the list
	private OnLoadMoreListener mOnLoadMoreListener;
	// To know if the list is loading more items
	private boolean hasMore = true;
	private int mCurrentScrollState;

	public LoadMoreListView(Context context) {
		super(context);
		init(context);
	}

	public LoadMoreListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {

		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mFooterView = (LinearLayout) mInflater.inflate(R.layout.load_more_footer, this, false);
		mFooterView.setVisibility(View.GONE);
		addFooterView(mFooterView);
		this.setSelector(R.color.touming);
		
		super.setOnScrollListener(this);
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
	}

	/**
	 * Set the listener that will receive notifications every time the list
	 * scrolls.
	 * 
	 * @param l
	 *            The scroll listener.
	 */
	@Override
	public void setOnScrollListener(AbsListView.OnScrollListener l) {
		mOnScrollListener = l;
	}

	/**
	 * Register a callback to be invoked when this list reaches the end (last
	 * item be visible)
	 * 
	 * @param onLoadMoreListener
	 *            The callback to run.
	 */

	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		if (mOnScrollListener != null) {
			mOnScrollListener.onScroll(view, firstVisibleItem,
					visibleItemCount, totalItemCount);
		}
		if (firstVisibleItem == 0 && visibleItemCount == 0
				&& totalItemCount == 0) {
			return;
		}

		if (mOnLoadMoreListener != null) {

			boolean loadMore = firstVisibleItem + visibleItemCount == totalItemCount;

			if (hasMore && loadMore && mCurrentScrollState != SCROLL_STATE_IDLE) {

				if (mFooterView.getVisibility() == View.GONE) {
					mFooterView.setVisibility(View.VISIBLE);
					onLoadMore();
				}

			} else {
				if (mFooterView.getVisibility() == View.VISIBLE) {
					mFooterView.setVisibility(View.GONE);
				}
			}

		}

	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		mCurrentScrollState = scrollState;

		if (mOnScrollListener != null) {
			mOnScrollListener.onScrollStateChanged(view, scrollState);
		}

	}

	private void onLoadMore() {
		if(Constant.debug)Log.d(TAG, "onLoadMore");
		if (mOnLoadMoreListener != null) {
			mOnLoadMoreListener.onLoadMore();
		}
	}

	/**
	 * Notify the loading more operation has complete
	 */
	public void loadComplete(boolean hasmore) {
		this.hasMore = hasmore;
		if (!hasmore) {
			mFooterView.setVisibility(View.GONE);
			this.removeFooterView(mFooterView);
			this.setOnScrollListener(null);
			mOnScrollListener = null;
		}
	}

	/**
	 * Interface definition for a callback to be invoked when list reaches the
	 * last item (the user load more items in the list)
	 */
	public interface OnLoadMoreListener {
		/**
		 * Called when the list reaches the last item (the last item is visible
		 * to the user)
		 */
		public void onLoadMore();
	}

}